package com.example.finaltool.service;

import com.example.finaltool.model.AreaInfo;
import com.example.finaltool.model.CunDepInfo;
import com.example.finaltool.model.config.Data;
import com.example.finaltool.model.DepInfo;
import com.example.finaltool.utils.FileUtils;
import com.example.finaltool.utils.PrintUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dengbw
 */
@Slf4j
@Service
public class DeployService {

    AdminAreaService adminAreaService;
    CmmFunctionConfigService cmmFunctionConfigService;
    CmmFunctionExtConfigService cmmFunctionExtConfigService;
    DingCommonService dingCommonService;
    MenuService menuService;
    SysAreaService sysAreaService;
    SysConfigService sysConfigService;
    SysCorpService sysCorpService;
    SysCorpStationService sysCorpStationService;
    SysCorpVillageService sysCorpVillageService;

    @Autowired
    public DeployService(DingCommonService dingCommonService, SysCorpService sysCorpService,
                         CmmFunctionExtConfigService cmmFunctionExtConfigService,
                         CmmFunctionConfigService cmmFunctionConfigService, MenuService menuService,
                         SysCorpStationService sysCorpStationService, SysAreaService sysAreaService,
                         SysConfigService sysConfigService, SysCorpVillageService sysCorpVillageService,
                         AdminAreaService adminAreaService) {
        this.adminAreaService = adminAreaService;
        this.dingCommonService = dingCommonService;
        this.sysCorpService = sysCorpService;
        this.cmmFunctionExtConfigService = cmmFunctionExtConfigService;
        this.cmmFunctionConfigService = cmmFunctionConfigService;
        this.menuService = menuService;
        this.sysCorpStationService = sysCorpStationService;
        this.sysAreaService = sysAreaService;
        this.sysConfigService = sysConfigService;
        this.sysCorpVillageService = sysCorpVillageService;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deploy(String filePath, boolean goDepend) throws IOException {
        PrintUtils.start();
        String excelPath = "";
        if (goDepend) {
            excelPath = FileUtils.findExcel(new File(filePath));
            FileUtils.transferData(filePath, excelPath);
        }
        Data data = new Data();
        File dataFile;
        dataFile = FileUtils.fillData(filePath, data);
        Map<String, String> core = dingCommonService.fetchCore(data);
        DepInfo info = dingCommonService.fetchDepInfo(data.getAppKey(), data.getAppSecret());
        save(data, core, info, dataFile);
        sysCorpService.enableSync(data.getCorpId());
        cmmFunctionConfigService.updateDefaultImage(data.getCorpId(), info.otherIds());
        FileUtils.saveCoreToText(dataFile, core, info);
        if (goDepend) {
            FileUtils.transferUser(filePath, excelPath);
        }
        PrintUtils.finish();
    }

    private void save(Data data, Map<String, String> core, DepInfo info, File dataFile) throws IOException {
        AreaInfo areaInfo = sysAreaService.save(data);
        sysCorpService.save(data, areaInfo);
        Map<String, String> adminAreaIds = adminAreaService.getAreaIds(data.getShi(), data.getQuxian(), data.getZhen(), info.getCunDepInfoList().stream().map(CunDepInfo::getCunName).collect(Collectors.toList()), dataFile);
        sysCorpVillageService.save(data, info, adminAreaIds);
        cmmFunctionExtConfigService.save(data, core, info);
        cmmFunctionConfigService.save(data, core, info);
        sysConfigService.save(data, info);
        menuService.save(data, info);
        sysCorpStationService.save(data, info);
    }
}
